package com.echo.ssm.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.echo.ssm.model.BkRole;
import com.echo.ssm.model.BkUser;
import com.echo.ssm.model.BkUserRole;
import com.echo.ssm.service.PowerMangerService;
import com.github.pagehelper.Page;

@Controller
@RequestMapping("/powerManger")
public class PowerMangerController {
private  final  static Logger  log=LoggerFactory.getLogger(PowerMangerController.class);
@Autowired
private  PowerMangerService  mangerService;

		@RequestMapping(value="/indexUser")
		private  String  indexUser(){
			log.info("进入user管理主页面");
		return "powermanger/indexUser";
		}

	@RequestMapping(value="/selectUserList")
	private  String selecetUserList(Integer pageSize,Integer pageNum ,  Model model){
		Page<BkUser> page = mangerService.selectkUser(pageSize,pageNum);
		model.addAttribute("userList", page.getResult());
		model.addAttribute("pageNum", page.getPageNum());
		model.addAttribute("totalPage", page.getPages());
		return "powermanger/dataUser";
	}
	@RequestMapping(value="/saveUser")
	private  String saveUser(BkUser  user,Model model){
	    int  flag=    mangerService.addUse(user);
	 return "{'result':"+flag+"}";
	}

	@RequestMapping(value="/selectUser")
	private  String selectUser(String userId,Model model){
	    BkUser  user=    mangerService.selectById(userId);
	    model.addAttribute("user", user);
	 return "powermanger/updateUser";
	}

@RequestMapping(value="/updateUser")
	private  String updateUser(BkUser  user){
	    int   flag=    mangerService.updateUser(user);
	    log.info(flag+"----更新数据");
	    
	    return "powermanger/result";
	}
@RequestMapping(value="/deletUser")
@ResponseBody
private  String deletUser(String   userId){
    int   flag=    mangerService.deleteUser(userId);
    return String.valueOf(flag);
}

@RequestMapping(value="/selectRoleList")
private  String selectRoleList(String userId,Model model){
     List<BkRole> listRole = mangerService.selectRole();
     model.addAttribute("listRole", listRole);
     model.addAttribute("userId", userId);
    return "powermanger/setUserRole";
}


@RequestMapping(value="/saveUserRole")
@ResponseBody
private  String selectRoleList(BkUserRole  userRole){
	  //保证数据唯一性 一个用户对应一种角色 
	  mangerService.deleteUserRole(userRole.getUserId());
	  int   flag=    mangerService.addBkUserRole(userRole);
	 return String.valueOf(flag);
}












/**
 * 准备分配角色的必须数据
 * @param model
 * @return
 */
@RequestMapping(value="/toUserRole")
private  String saveBkUserRoleData(Model model){
	  model.addAttribute("userList", mangerService.selectkUser(1,10));
	  model.addAttribute("roleList",mangerService.selectRole());
  return  "powermanger/addUserRole" ;
 }


/**
 * 添加角色
 * @param userRole
 * @param model
 * @return
 */
@RequestMapping(value="/userRole")
@ResponseBody
private  String saveBkUserRoleData(BkUserRole  userRole,Model model){
	     mangerService.addBkUserRole(userRole);
  return "{'result':1}";
 }
}
